package com.changgou.business.controller;

import com.changgou.business.pojo.Ad;
import com.changgou.business.service.Adservice;
import com.changgou.common.pojo.PageResult;
import com.changgou.common.pojo.Result;
import com.changgou.common.pojo.StatusCode;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
@RequestMapping("/ad")
public class AdController {
    @Autowired
    private Adservice adservice;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public Result findAll(){
        List<Ad> adList = adservice.findAll();
        return  new Result(true, StatusCode.OK, "查询成功",adList);
    }

    /**
     * 根据id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public  Result findById(@PathVariable Integer id ){
        Ad ad = adservice.findAdById(id);
        return  new Result(true, StatusCode.OK, "查询成功",ad);
    }

    /**
     * 新增数据
     * @param ad
     * @return
     */
    @PostMapping
    public  Result add(@RequestBody Ad ad ){
        adservice.add(ad);
        return  new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 修改数据
     * @param ad
     * @return
     */
    @PutMapping(value = "/{id}")
    public  Result update(@RequestBody Ad ad ,@PathVariable Integer id){
        ad.setId(id);
        adservice.update(ad);
        return  new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping(value = "/{id}")
    public  Result delete(@PathVariable Integer id){
        adservice.delete(id);
        return  new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * 分页查询
     * @param page
     * @param size
     * @return
     */
    @GetMapping("/search/{page}/{size}")
    public  Result findPage(@PathVariable("page") int page,@PathVariable("size") int size){
        Page<Ad> pageInfo = adservice.findPage(page, size);
        PageResult pageResult = new PageResult(pageInfo.getTotal(),pageInfo.getResult());
        return new Result(true,StatusCode.OK,"查询成功",pageResult);
    }

    /**
     * 多条件查询
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/search")
    public  Result findPage(@PathVariable Map searchMap){
        List<Ad> list = adservice.findList(searchMap);
        return  new Result(true, StatusCode.OK, "查询成功",list);
    }

    /**
     * 多条件分成也查询的实现
     * @param page
     * @param size
     * @param searchMap
     * @return
     */
    @GetMapping(value = "/searchPage/{page}/{size}" )
    public  Result findPage(@PathVariable int page,@PathVariable int size,@PathVariable Map searchMap){
        Page<Ad> page1 = adservice.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(page1.getTotal(), page1.getResult());
        return  new Result(true, StatusCode.OK, "查询成功",pageResult);
    }

}
